package com.hkts.management.utils;

import com.alibaba.fastjson.JSONObject;
import com.hkts.management.common.vo.RegionConstants;
import com.hkts.management.feign.FrontInterface;
import com.hkts.management.model.result.ResultAreaPlace;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RegionConverUtil {
    @Autowired
    private FrontInterface frontInterface;
    public ResultAreaPlace regionCover(Integer regionCode, ResultAreaPlace areaPlace){
        String result= getRegionRedisBycode(regionCode);
        log.info("result{}",result);
        if(result!=null&&!"".equals(result)){
            HashedMap map= JSONObject.parseObject(result, HashedMap.class);
            areaPlace.setProvice(map.get(RegionConstants.PROVINCE).toString());
            areaPlace.setCity(map.get(RegionConstants.CITY).toString());
            areaPlace.setCounty(map.get(RegionConstants.COUNTY).toString());
        }
        return areaPlace;
    }
    public HashedMap regionCoverMap(Integer regionCode){
        String result= getRegionRedisBycode(regionCode);
        log.info("result{}",result);
        if(result!=null&&!"".equals(result)){
            //            areaPlace.setProvice(map.get(RegionConstants.PROVINCE).toString());
//            areaPlace.setCity(map.get(RegionConstants.CITY).toString());
//            areaPlace.setCounty(map.get(RegionConstants.COUNTY).toString());
            return JSONObject.parseObject(result, HashedMap.class);
        }
        return null;
    }
    public String regionCoverStr(Integer regionCode){
        String result= getRegionRedisBycode(regionCode);
        log.info("result{}",result);
        String region="";
        if(result!=null&&!"".equals(result)){
            HashedMap   map=JSONObject.parseObject(result, HashedMap.class);
            region+=map.get(RegionConstants.PROVINCE).toString();
            region+=map.get(RegionConstants.CITY).toString();
            region+=map.get(RegionConstants.COUNTY).toString();
        }
        return region;
    }
    public String getRegionRedisBycode(Integer code){
        return  frontInterface.getregionRedisByCode(code);
    }
    public Integer regionparam(String code){
        Integer endRegion=0;
        Integer regionCode=Integer.parseInt(code);
        String result= getRegionRedisBycode(regionCode);
        if(result!=null&&!"".equals(result)){
            HashedMap map= JSONObject.parseObject(result, HashedMap.class);
            Integer regionType=Integer.valueOf(map.get(RegionConstants.REGION_TYPE).toString());
            switch (regionType){
                case 1:
                    endRegion=regionCode+10000;
                    break;
                case 2:
                    endRegion=regionCode+100;
                    break;
                case 3:
                    endRegion=0;
                    break;
            }
        }
        return endRegion;
    }
}

